export default function(app) {
  app.service('ngDialogProblem', ngDialogProblem);

  function ngDialogProblem($q, ngDialog) {
    'ngInject';

    this.confirm = (id) => {
      let deferred = $q.defer();

      ngDialog.open({
        template: 'rectification',
        className: 'ngdialog-theme-default pa-special-check-item-list-theme rectification',
        controllerAs: 'rectificationDetail',
        controller: function rectificationCtrl($rootScope, $scope, $http) {
          'ngInject';

          let _this = this;
          this.isUploading = false;

          this.reasons = [{
            id: 1,
            name: '管理制度',
            active: false
          }, {
            id: 2,
            name: '岗位职责',
            active: false
          }, {
            id: 3,
            name: '专项方案',
            active: false
          }, {
            id: 3,
            name: '安全措施',
            active: false
          }, {
            id: 4,
            name: '思想行为',
            active: false
          }, {
            id: 5,
            name: '经费保障',
            active: false
          }, {
            id: 6,
            name: '其他',
            active: false
          }];

          this.rectificationList = {};
          this.detailList = {
            problemModifyBean: {
              fileBeanList: [],
            },
          };
          // 获取专项检查详情-问题记录
          this.getSpecialDetail = () => {
            $http.get(`${SERVER_CONTEXT}/api/checkRecord/app/queryContentDetail/${id}`)
              .then((resp) => {
                this.detailList = resp.data;
                this.rectificationList = this.detailList.problemModifyBean ? this.detailList.problemModifyBean : {};
                this.approval = this.detailList.problemModifyBean ? this.detailList.problemModifyBean : {};
                handleProblemReasons2ViewModel(resp.data);

                function handleProblemReasons2ViewModel(respData) {
                  if (respData && respData.problemModifyBean && respData.problemModifyBean.causeAnalysis) {
                    let activeReasons = respData.problemModifyBean.causeAnalysis.split(',');
                    activeReasons.forEach((activeReason) => {
                      _this.reasons.forEach((reason) => {
                        if (reason.name == activeReason) {
                          reason.active = true;
                        }
                      })
                    });
                  }
                }
              });
          }

          // 上传新增整改后照片
          this.submit = () => {
            var causeAnalysis = "";
            // 上传图片
            let fileBeanList = (this.detailList.problemModifyBean && this.detailList.problemModifyBean.fileBeanList) || [];
            // modifyImage 属性用于接收附件token 多个附件以逗号分隔

            this.rectificationList.problemImageIds = fileBeanList.map(item => item.id).join(',');
            this.rectificationList.problemId = id;

            // 原因分析
            this.reasons.forEach(function(item, index) {
              if (item.active) {
                causeAnalysis = causeAnalysis ? causeAnalysis + ',' + item.name : item.name;
              }
            });

            this.rectificationList.causeAnalysis = causeAnalysis;

            console.log(this.rectificationList);

            $http.post(`${SERVER_CONTEXT}/api/checkRecord/app/addProblemModify`,
                this.rectificationList
              )
              .then((resp) => {
                if (resp.data.success) {
                  layer.msg(resp.data.message);
                  this.getSpecialDetail();
                  deferred.resolve(resp);
                } else {
                  layer.alert(resp.data.message);
                  deferred.reject(resp);
                }
              });
          }

          // 展示审批
          this.isAuditable = function() {
            if (this.detailList.problemFoundUserId == $rootScope.userInfo.userId && this.detailList.problemStatus == 1) {
              return true;
            }
            return false;
          }

          // 提交审批意见
          this.approvalSubmit = () => {
            let reqData = {
              checkResult: parseInt(this.approval.checkResult),
              problemId: id,
              comments: this.approval.comments
            }

            $http.post(`${SERVER_CONTEXT}/api/checkRecord/app/auditModifyProblem`,
                reqData
              )
              .then((resp) => {
                if (resp.data.success) {
                  layer.msg(resp.data.message);
                  // $state.reload();
                  this.getSpecialDetail();
                  deferred.resolve(resp);
                } else {
                  layer.alert(resp.data.message);
                  deferred.reject(resp);
                }
              });
          }

          // 文件预览
          this.preview = (archiveDetail) => {
            let url = $state.href('documentPreview', {
              id: archiveDetail.id,
            });
            window.open(url,'_blank');
          }

          // 重置审批意见
          this.reset = () => {
            this.approval = {};
          }

          this.initPage = () => {
            // 获取问题详情
            this.getSpecialDetail();
          }

          // 初始化页面
          this.initPage();

        }
      });
      return deferred.promise;
    }
  }
}